const axios = require('axios');

const baseURL = 'http://localhost:7002/api/v1';

// 测试用的token
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJkZW1vIiwiZW1haWwiOiJkZW1vQGV4YW1wbGUuY29tIiwiaWF0IjoxNzUzMjM0MTc1LCJleHAiOjE3NTM4Mzg5NzV9.eH9yAWLfyrJVCUB-DqhI1Z5aaxtiED72oRZTESf-7Qs';

const api = axios.create({
  baseURL,
  headers: {
    'Authorization': `Bearer ${token}`,
    'Content-Type': 'application/json'
  }
});

async function testColdWeatherRecommendations() {
  console.log('❄️ 测试严寒天气推荐算法...\n');

  try {
    // 1. 测试雪天 -3°C 推荐
    console.log('1️⃣ 测试雪天 -3°C 推荐...');
    try {
      const snowRes = await api.get('/recommendation/weather-based', {
        params: { 
          city: '北京', 
          weather: '雪', 
          temperature: -3 
        }
      });
      
      console.log(`✅ 雪天推荐成功，获得 ${snowRes.data.data.recommendations?.length || 0} 个推荐`);
      
      if (snowRes.data.data.recommendations?.length > 0) {
        snowRes.data.data.recommendations.forEach((rec, index) => {
          console.log(`\n   推荐 ${index + 1}: ${rec.name || '无名称'}`);
          console.log(`   推荐理由: ${rec.reason || '无'}`);
          console.log(`   包含 ${rec.items?.length || 0} 件衣服:`);
          
          rec.items?.forEach(item => {
            const category = item.category?.name || '未知分类';
            console.log(`     - ${item.name}(${item.color || '无色'}) [${category}]`);
          });
          
          // 检查是否有不合适的单品
          const unsuitableItems = rec.items?.filter(item => {
            const name = item.name.toLowerCase();
            return name.includes('西装') || name.includes('薄') || 
                   name.includes('短袖') || name.includes('凉鞋') ||
                   name.includes('拖鞋');
          });
          
          if (unsuitableItems && unsuitableItems.length > 0) {
            console.log(`   ⚠️ 发现不适合严寒天气的单品:`);
            unsuitableItems.forEach(item => {
              console.log(`     ❌ ${item.name} - 不适合-3°C雪天`);
            });
          } else {
            console.log(`   ✅ 所有单品都适合严寒天气`);
          }
        });
      } else {
        console.log('   ⚠️ 没有生成推荐');
      }
    } catch (error) {
      console.error('❌ 雪天推荐失败:', error.response?.data?.message || error.message);
    }
    console.log('\n' + '='.repeat(60) + '\n');

    // 2. 测试严寒天气 -10°C 推荐
    console.log('2️⃣ 测试严寒天气 -10°C 推荐...');
    try {
      const coldRes = await api.get('/recommendation/weather-based', {
        params: { 
          city: '哈尔滨', 
          weather: '晴', 
          temperature: -10 
        }
      });
      
      console.log(`✅ 严寒推荐成功，获得 ${coldRes.data.data.recommendations?.length || 0} 个推荐`);
      
      if (coldRes.data.data.recommendations?.length > 0) {
        const firstRec = coldRes.data.data.recommendations[0];
        console.log(`\n   第一个推荐: ${firstRec.name || '无名称'}`);
        console.log(`   推荐理由: ${firstRec.reason || '无'}`);
        console.log(`   包含 ${firstRec.items?.length || 0} 件衣服:`);
        
        firstRec.items?.forEach(item => {
          const category = item.category?.name || '未知分类';
          console.log(`     - ${item.name}(${item.color || '无色'}) [${category}]`);
        });
      }
    } catch (error) {
      console.error('❌ 严寒推荐失败:', error.response?.data?.message || error.message);
    }
    console.log('\n' + '='.repeat(60) + '\n');

    // 3. 对比测试：温暖天气 25°C 推荐
    console.log('3️⃣ 对比测试：温暖天气 25°C 推荐...');
    try {
      const warmRes = await api.get('/recommendation/weather-based', {
        params: { 
          city: '广州', 
          weather: '晴', 
          temperature: 25 
        }
      });
      
      console.log(`✅ 温暖推荐成功，获得 ${warmRes.data.data.recommendations?.length || 0} 个推荐`);
      
      if (warmRes.data.data.recommendations?.length > 0) {
        const firstRec = warmRes.data.data.recommendations[0];
        console.log(`\n   第一个推荐: ${firstRec.name || '无名称'}`);
        console.log(`   推荐理由: ${firstRec.reason || '无'}`);
        console.log(`   包含 ${firstRec.items?.length || 0} 件衣服:`);
        
        firstRec.items?.forEach(item => {
          const category = item.category?.name || '未知分类';
          console.log(`     - ${item.name}(${item.color || '无色'}) [${category}]`);
        });
        
        // 检查是否有过厚的单品
        const unsuitableItems = firstRec.items?.filter(item => {
          const name = item.name.toLowerCase();
          return name.includes('羽绒') || name.includes('毛衣') || 
                 name.includes('厚') || name.includes('棉衣') ||
                 name.includes('靴子');
        });
        
        if (unsuitableItems && unsuitableItems.length > 0) {
          console.log(`   ⚠️ 发现不适合温暖天气的单品:`);
          unsuitableItems.forEach(item => {
            console.log(`     ❌ ${item.name} - 不适合25°C温暖天气`);
          });
        } else {
          console.log(`   ✅ 所有单品都适合温暖天气`);
        }
      }
    } catch (error) {
      console.error('❌ 温暖推荐失败:', error.response?.data?.message || error.message);
    }

    console.log('\n🎉 严寒天气推荐测试完成！');

  } catch (error) {
    console.error('❌ 测试过程中发生错误:', error.message);
  }
}

// 运行测试
testColdWeatherRecommendations();
